package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.activity.foxcard.*;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardShowInfo;
import com.yanqu.road.pb.activity.FoxCardProto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FoxCardPb {
    public static FoxCardProto.FoxCardUserDataSyncMsg.Builder parseUserSyncMsg(UserFoxCardData userData, UserFoxCardGameData gameData) {
        FoxCardProto.FoxCardUserDataSyncMsg.Builder builder = FoxCardProto.FoxCardUserDataSyncMsg.newBuilder();
        builder.setActivityId(userData.getActivityId());
        builder.setGameTimes(userData.getGameTimes());
        builder.setLastGameTime(userData.getLastGameTime());
        builder.setPower(userData.getPower());
        builder.setPowerRecoverTime(userData.getPowerRecoverTime());
        builder.setTreeExp(userData.getTreeExp());
        builder.setTreeLevel(userData.getTreeLevel());
        builder.setTreeScore(userData.getTreeScore());
        builder.addAllTreeCards(parseTreeCardTempList(userData.getTreeCards()));
        builder.addAllUnlockCards(userData.getUnlockCards());
        builder.addAllEquipCards(userData.getEquipCards());
        builder.setMaxScore(userData.getMaxScore().toString());
        builder.setInBattleStageId(gameData.getStageId());
        for (Map.Entry<Integer, Integer> entry : userData.getPassStages().entrySet()) {
            FoxCardProto.FoxCardPassStageTemp.Builder stageBuilder = FoxCardProto.FoxCardPassStageTemp.newBuilder();
            stageBuilder.setStageId(entry.getKey());
            stageBuilder.setMaxRound(entry.getValue());
            builder.addPassStages(stageBuilder);
        }
        builder.setGuide(userData.getGuide());
        builder.setRemainGuaranteeNum(userData.getGuaranteeAmount());
        builder.setIsGoldGuarantee(userData.isGoldenGuarantee());
        return builder;
    }

    public static List<FoxCardProto.FoxCardTreeCardTemp> parseTreeCardTempList(List<FoxTreeCard> treeCards) {
        List<FoxCardProto.FoxCardTreeCardTemp> treeCardTempList = new ArrayList<>();
        for (FoxTreeCard treeCard : treeCards) {
            FoxCardProto.FoxCardTreeCardTemp.Builder treeCardBuilder = FoxCardProto.FoxCardTreeCardTemp.newBuilder();
            treeCardBuilder.setLotteryId(treeCard.getLotteryId());
            treeCardBuilder.setCardId(treeCard.getCardId());
            treeCardBuilder.setPoint(treeCard.getPoint());
            treeCardBuilder.setScoreAdd(treeCard.getAddScore());
            treeCardTempList.add(treeCardBuilder.build());
        }
        return treeCardTempList;
    }

    public static FoxCardProto.FoxCardGameDataTemp.Builder parseGameBuilder(UserFoxCardGameData gameData) {
        FoxCardProto.FoxCardGameDataTemp.Builder builder = FoxCardProto.FoxCardGameDataTemp.newBuilder();
        builder.setStageId(gameData.getStageId());
        builder.setRound(gameData.getRound());
        builder.setEffectCardNumLimit(gameData.getEffectCardNumLimit());
        builder.setConsumeCardNumLimit(gameData.getConsumeCardNumLimit());
        builder.setHandCardNumLimit(gameData.getHandCardNumLimit());
        builder.addAllEffectCards(parseEffectCardTempList(gameData.getEffectCards()));
        builder.addAllConsumeCards(parseConsumeCardTempList(gameData.getConsumeCards()));
        builder.addAllHandCards(parseHandCardTempList(gameData.getHandCards()));
        builder.addAllRemainHandCards(parseHandCardTempList(gameData.getRemainHandCards()));
        builder.addAllDropHandCards(parseHandCardTempList(gameData.getDropHandCards()));
        builder.setShowCardTimes(gameData.getShowCardTimes());
        builder.setDropCardTimes(gameData.getDropCardTimes());
        builder.setGold(gameData.getGold());
        builder.setDamageAmount(gameData.getDamageAmount().toString());
        builder.addAllWaitChooseCards(parseSaleCardTempList(gameData.getWaitChooseCards()));
        builder.setRemainChoseTimes(gameData.getRemainChooseTimes());
        builder.addAllComboInfos(parseComboInfoTempList(gameData.getComboInfoMap()));
        builder.addAllCoupon(gameData.getCoupons());
        builder.setBossLimitEffect(gameData.getBossLimitEffect());
        builder.addAllSkipEffectHave(gameData.getSkipLabelHaves());
        builder.addAllConsumeCardUsed(parseHistoryTimesTempList(gameData.getConsumeCardTimesMap()));
        builder.addAllHistoryReinForeEffects(parseHistoryTimesTempList(gameData.getHistoryBuffTimesMap()));
        if (gameData.getIsShopping()) {
            builder.setShop(parseShopTemp(gameData.getShop()));
        }
        builder.addAllShowPointAmount(parseHistoryTimesTempList(gameData.getShowPointAmountMap()));
        builder.addAllDropPointAmount(parseHistoryTimesTempList(gameData.getDropPointAmountMap()));
        builder.setSkipTimesAmount(gameData.getSkipTimesAmount());
        builder.setShowTimesAmount(gameData.getShowTimesAmount());
        builder.setDropTimesAmount(gameData.getDropTimesAmount());
        builder.setLastUsedConsumeCardId(gameData.getLastUsedConsumeCardId());
        builder.setRoundLastShowCombo(gameData.getRoundLastShowCombo());
        builder.addAllRoundLimitEffectCard(gameData.getRoundLimitEffectCard());
        builder.addAllRoundShowCombos(gameData.getRoundShowCombos());
        builder.setMaxOnceDamage(gameData.getMaxOnceDamage().toString());
        return builder;
    }

    private static FoxCardProto.FoxCardGameShopTemp.Builder parseShopTemp(UserFoxCardShop shop) {
        FoxCardProto.FoxCardGameShopTemp.Builder shopBuilder = FoxCardProto.FoxCardGameShopTemp.newBuilder();
        shopBuilder.addAllSaleCards(parseSaleCardTempList(shop.getSaleCards()));
        shopBuilder.setRefreshAmount(shop.getRefreshAmount());
        shopBuilder.setRefreshPrice(shop.getRefreshPrice());
        if (shop.getFreeTimes() > 0) {
            shopBuilder.setRefreshPrice(0);
        }
        shopBuilder.addAllInPoolCardId(shop.getInPoolCardIds());
        shopBuilder.addAllOutPoolCardId(shop.getOutPoolCardIds());
        if (shop.getCoupon() != null) {
            shopBuilder.setCoupon(parseSaleCardTemp(shop.getCoupon()));
        }
        shopBuilder.addAllCardPacks(parseCardPackTempList(shop.getCardPacks()));
        return shopBuilder;
    }

    private static Iterable<FoxCardProto.FoxCardPackTemp> parseCardPackTempList(List<FoxCardPack> cardPacks) {
        List<FoxCardProto.FoxCardPackTemp> cardPackTempList = new ArrayList<>();
        for (FoxCardPack cardPack : cardPacks) {
            FoxCardProto.FoxCardPackTemp.Builder packBuilder = FoxCardProto.FoxCardPackTemp.newBuilder();
            packBuilder.setCardPackId(cardPack.getCardPackId());
            packBuilder.setPrice(cardPack.getPrice());
            packBuilder.setPackType(cardPack.getPackType());
            cardPackTempList.add(packBuilder.build());
        }
        return cardPackTempList;
    }

    public static FoxCardProto.FoxCardCardBaseTemp.Builder parseCardBaseTemp(FoxBaseCard baseCard) {
        FoxCardProto.FoxCardCardBaseTemp.Builder builder = FoxCardProto.FoxCardCardBaseTemp.newBuilder();
        builder.setId(baseCard.getId());
        builder.setType(baseCard.getCardType());
        builder.setPrice(baseCard.getPrice());
        return builder;
    }

    public static FoxCardProto.FoxCardEffectCardTemp.Builder parseEffectCardTemp(FoxEffectCard effectCard) {
        FoxCardProto.FoxCardEffectCardTemp.Builder builder = FoxCardProto.FoxCardEffectCardTemp.newBuilder();
        builder.setBase(parseCardBaseTemp(effectCard));
        builder.setConfigId(effectCard.getConfigId());
        builder.setVersionEffect(effectCard.getVersionEffect());
        builder.addAllEffectValue(effectCard.getEffectValues());
        return builder;
    }

    public static FoxCardProto.FoxCardConsumeCardTemp.Builder parseConsumeCardTemp(FoxConsumeCard consumeCard) {
        FoxCardProto.FoxCardConsumeCardTemp.Builder builder = FoxCardProto.FoxCardConsumeCardTemp.newBuilder();
        builder.setBase(parseCardBaseTemp(consumeCard));
        builder.setConfigId(consumeCard.getConfigId());
        builder.setVersionEffect(consumeCard.getVersionEffect());
        return builder;
    }

    public static FoxCardProto.FoxCardHandCardTemp.Builder parseHandCardTemp(FoxHandCard handCard) {
        FoxCardProto.FoxCardHandCardTemp.Builder builder = FoxCardProto.FoxCardHandCardTemp.newBuilder();
        builder.setBase(parseCardBaseTemp(handCard));
        builder.setOriPoint(handCard.getOriPoint());
        builder.setOriColor(handCard.getOriColor());
        builder.setPoint(handCard.getPoint());
        builder.setColor(handCard.getColor());
        builder.setReinforceEffect(handCard.getReinforceEffect());
        builder.setVersionEffect(handCard.getVersionEffect());
        builder.setSealEffect(handCard.getSealEffect());
        return builder;
    }

    public static List<FoxCardProto.FoxCardEffectCardTemp> parseEffectCardTempList(List<FoxEffectCard> effectCards) {
        List<FoxCardProto.FoxCardEffectCardTemp> effectCardTempList = new ArrayList<>();
        for (FoxEffectCard effectCard : effectCards) {
            effectCardTempList.add(parseEffectCardTemp(effectCard).build());
        }
        return effectCardTempList;
    }

    public static List<FoxCardProto.FoxCardConsumeCardTemp> parseConsumeCardTempList(List<FoxConsumeCard> consumeCards) {
        List<FoxCardProto.FoxCardConsumeCardTemp> consumeCardTempList = new ArrayList<>();
        for (FoxConsumeCard consumeCard : consumeCards) {
            consumeCardTempList.add(parseConsumeCardTemp(consumeCard).build());
        }
        return consumeCardTempList;
    }

    public static List<FoxCardProto.FoxCardHandCardTemp> parseHandCardTempList(List<FoxHandCard> handCards) {
        List<FoxCardProto.FoxCardHandCardTemp> handCardTempList = new ArrayList<>();
        for (FoxHandCard handCard : handCards) {
            handCardTempList.add(parseHandCardTemp(handCard).build());
        }
        return handCardTempList;
    }

    public static FoxCardProto.FoxCardSaleCardTemp.Builder parseSaleCardTemp(FoxSaleCard saleCard) {
        FoxCardProto.FoxCardSaleCardTemp.Builder builder = FoxCardProto.FoxCardSaleCardTemp.newBuilder();
        builder.setId(saleCard.getId());
        builder.setConfigId(saleCard.getConfigId());
        builder.setPrice(saleCard.getPrice());
        builder.setType(saleCard.getType());
        builder.setPoint(saleCard.getPoint());
        builder.setColor(saleCard.getColor());
        builder.setReinforceEffect(saleCard.getReinforceEffect());
        builder.setVersionEffect(saleCard.getVersionEffect());
        builder.setSealEffect(saleCard.getSealEffect());
        return builder;
    }

    public static List<FoxCardProto.FoxCardSaleCardTemp> parseSaleCardTempList(List<FoxSaleCard> saleCards) {
        List<FoxCardProto.FoxCardSaleCardTemp> saleCardTempList = new ArrayList<>();
        for (FoxSaleCard saleCard : saleCards) {
            saleCardTempList.add(parseSaleCardTemp(saleCard).build());
        }
        return saleCardTempList;
    }

    public static List<FoxCardProto.FoxCardCardComboInfoTemp> parseComboInfoTempList(Map<Integer, List<Integer>> comboInfo) {
        List<FoxCardProto.FoxCardCardComboInfoTemp> comboInfoTempList = new ArrayList<>();
        for (Map.Entry<Integer, List<Integer>> entry : comboInfo.entrySet()) {
            FoxCardProto.FoxCardCardComboInfoTemp.Builder builder = FoxCardProto.FoxCardCardComboInfoTemp.newBuilder();
            Integer comboId = entry.getKey();
            int level = entry.getValue().get(0);
            int showTimes = entry.getValue().get(1);
            builder.setComboId(comboId);
            builder.setLevel(level);
            builder.setShowTimes(showTimes);
            comboInfoTempList.add(builder.build());
        }
        return comboInfoTempList;
    }

    public static List<FoxCardProto.FoxCardHistoryTimesTemp> parseHistoryTimesTempList(Map<Integer, Integer> timesMap) {
        List<FoxCardProto.FoxCardHistoryTimesTemp> tempList = new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : timesMap.entrySet()) {
            FoxCardProto.FoxCardHistoryTimesTemp.Builder builder = FoxCardProto.FoxCardHistoryTimesTemp.newBuilder();
            builder.setTypeId(entry.getKey());
            builder.setNum(entry.getValue());
            tempList.add(builder.build());
        }
        return tempList;
    }

    public static List<FoxCardProto.FoxCardMonsterTemp> parseMonsterTempList(List<UserFoxCardMonster> monsters) {
        List<FoxCardProto.FoxCardMonsterTemp> monsterTempList = new ArrayList<>();
        for (UserFoxCardMonster monster : monsters) {
            FoxCardProto.FoxCardMonsterTemp.Builder builder = FoxCardProto.FoxCardMonsterTemp.newBuilder();
            builder.setRound(monster.getRound());
            builder.setSkipEffect(monster.getSkipEffect());
            builder.setBossLimitEffect(monster.getBossLimitEffect());
            builder.setHp(monster.getHp().toString());
            monsterTempList.add(builder.build());
        }
        return monsterTempList;
    }

    public static FoxCardProto.FoxCardShowTemp.Builder parseShowTemp(FoxCardShowInfo showInfo) {
        FoxCardProto.FoxCardShowTemp.Builder builder = FoxCardProto.FoxCardShowTemp.newBuilder();
        builder.setShowType(showInfo.getShowType());
        for (List<Integer> list : showInfo.getSettleCards()) {
            FoxCardProto.FoxCardCardShowTemp.Builder cardShowTemp = FoxCardProto.FoxCardCardShowTemp.newBuilder();
            cardShowTemp.setId(list.get(0));
            cardShowTemp.setType(list.get(1));
            builder.addSettleCard(cardShowTemp.build());
        }
        for (List<Integer> list : showInfo.getTriggerCards()) {
            FoxCardProto.FoxCardCardShowTemp.Builder cardShowTemp = FoxCardProto.FoxCardCardShowTemp.newBuilder();
            cardShowTemp.setId(list.get(0));
            cardShowTemp.setType(list.get(1));
            builder.addTriggerCard(cardShowTemp.build());
        }
        builder.setNowBaseScore(showInfo.getNowBaseScore().toString());
        builder.setNowMulti(showInfo.getNowBaseMulti().toString());
        builder.setShowValue(showInfo.getShowValue());
        builder.setDamageAmount(showInfo.getDamageAmount().toString());
        builder.setTriggerType(showInfo.getTriggerType());
        return builder;
    }

    public static List<FoxCardProto.FoxCardShowTemp> parseShowTempList(List<FoxCardShowInfo> showInfos) {
        List<FoxCardProto.FoxCardShowTemp> showTempList = new ArrayList<>();
        for (FoxCardShowInfo showInfo : showInfos) {
            showTempList.add(parseShowTemp(showInfo).build());
        }
        return showTempList;
    }
}
